// name=value;expires=xx;path=xxx;domain=xx;secure;
function setCookie(name, value, num, path, domain, secure) {
    var strCookie = "";
    if (name) {
        strCookie += encodeURIComponent(name) + "=" + encodeURIComponent(value) + ";";
    }

    if (typeof (num) == "number") {
        var date = new Date();
        date.setDate(date.getDate() + num); //保存多少天
        strCookie += "expires=" + date.toUTCString() + ";";
    }

    if (path) {
        strCookie += "path=" + path + ";"
    }

    if (domain) {
        strCookie += "domain=" + domain + ";"
    }

    if (secure) {
        strCookie += "secure"
    }

    document.cookie = strCookie;

}

//1.获取cookie所有的数据
function getCookieAll() {

    var reusltObj = {};
    var cookieStr = decodeURIComponent(document.cookie);

    var arr = cookieStr.split(";"); //返回一个数组 [cs1901='[文雷,刘童]',abc=a123 ]

    for (var i = 0; i < arr.length; i++) {
        var tmpArr = arr[i].split("="); // [ cs1901, '[文雷,刘童]']

        var key = tmpArr[0].trim();
        var value = tmpArr[1]

        reusltObj[key] = value;
    }
    return reusltObj;
}

//2.通过制定的name获取对应value
function getCookieByName(name) {
    var obj = getCookieAll();
    return obj[name];
}


function removeCookie(name) {
    var date = new Date();
    date.setDate(date.getDate() - 1);
    if (name) {
        document.cookie = encodeURIComponent(name) + "= ;expires=" + date.toUTCString()
    }
}